/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.anote.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import javax.swing.*;
import org.anote.Note;

/**
 *
 * @author Trilarion 2012
 */
public class NotePanel {

    private JPanel panel;
    private JDialog dialog;
    private Note note;
    private JLabel title;
    private JScrollPane textScroller;
    private JTextArea text;

    public NotePanel(Note note) {
        this.note = note;

        panel = new JPanel(new LayoutManager() {

            /*
             * Required by LayoutManager.
             */
            @Override
            public void addLayoutComponent(String name, Component comp) {
            }

            /*
             * Required by LayoutManager.
             */
            @Override
            public void removeLayoutComponent(Component comp) {
            }

            @Override
            public Dimension preferredLayoutSize(Container parent) {
                Insets insets = parent.getInsets();
                Dimension dim = new Dimension();
                Dimension dimA = title.getPreferredSize();

                dim.width = insets.left + insets.right + 80; // at least 80 pixel wide
                dim.height = insets.top + insets.bottom + dimA.height; // at least title should be displayed

                return dim;
            }

            @Override
            public Dimension minimumLayoutSize(Container parent) {
                Insets insets = parent.getInsets();
                Dimension dim = new Dimension();
                Dimension dimA = title.getPreferredSize();

                dim.width = insets.left + insets.right + 80; // at least 80 pixel wide
                dim.height = insets.top + insets.bottom + dimA.height; // at least title should be displayed

                return dim;
            }

            @Override
            public void layoutContainer(Container parent) {
                Insets insets = parent.getInsets();
                int x0 = insets.left;
                int y0 = insets.top;
                Dimension dimA = title.getPreferredSize();
                Dimension dimB = textScroller.getPreferredSize();

                int maxWidth = parent.getWidth() - (insets.left + insets.right);
                int maxHeight = parent.getHeight() - (insets.top + insets.bottom);

                // put title on the left uper corner
                title.setBounds(x0, y0, Math.min(maxWidth, dimA.width), Math.min(maxHeight, dimA.height));

                // put text box below, filling the whole width and the remaining height
                textScroller.setBounds(x0, y0 + dimA.height, maxWidth, Math.max(0, maxHeight - dimA.height));
            }
        });
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panel.setBackground(Color.YELLOW);

        title = new JLabel(note.getTitle());
        panel.add(title);

        text = new JTextArea();
        text.setColumns(20);
        text.setRows(5);
        text.setLineWrap(true);
        text.setBackground(Color.YELLOW);

        textScroller = new JScrollPane();
        textScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        textScroller.setViewportView(text);
        panel.add(textScroller);
    }

    JDialog getAsDialog() {
        if (dialog == null) {
            // create dialog and add panel
            dialog = new JDialog();
            dialog.setUndecorated(true);
            dialog.getRootPane().getWindowDecorationStyle();
            dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
            dialog.add(panel);
            dialog.setLocation(note.getPosition());
            dialog.pack();
        }
        return dialog;
    }
}
